<template>
  <view class="touxiang-page">
    <!-- 返回入口页 -->
    <view class="back-btn" @click="navigateBack">
      <uni-icons type="left" size="36rpx" color="#333"></uni-icons>
      <text class="back-text">返回</text>
    </view>

    <!-- 仅在此页面支持点击头像选图 -->
    <view class="avatar-container">
      <view class="avatar-title">个人头像设置</view>
      <view class="avatar-wrapper" @click="chooseAvatar">
        <image :src="avatarUrl" mode="aspectFill" class="avatar-img"></image>
        <view class="avatar-edit-icon">
          <uni-icons type="camera" size="36rpx" color="#fff"></uni-icons>
        </view>
      </view>
      <text class="tips">点击头像可从相册选择或拍摄照片</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      avatarUrl: 'https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_1280.png'
    }
  },
  onLoad() {
    // 加载已保存的头像（持久化）
    this.loadPersistentAvatar();
  },
  methods: {
    // 返回入口页
    navigateBack() {
      uni.navigateBack({ delta: 1 });
    },

    // 唯一的选图逻辑：仅在此页面触发
    chooseAvatar() {
      uni.chooseImage({
        count: 1, // 仅选1张
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'], // 相册/拍摄
        success: (res) => {
          const tempPath = res.tempFilePaths[0];
          this.saveAvatarToLocal(tempPath); // 持久化保存
        },
        fail: (err) => {
          console.error('选择头像失败：', err);
          uni.showToast({ title: '选择失败，请重试', icon: 'none' });
        }
      });
    },

    // 持久化保存头像（重启不丢失）
    saveAvatarToLocal(tempPath) {
      const fs = uni.getFileSystemManager();
      const avatarPath = `${uni.env.USER_DATA_PATH}/user_avatar.jpg`;

      fs.copyFile({
        srcPath: tempPath,
        destPath: avatarPath,
        success: () => {
          this.avatarUrl = avatarPath;
          uni.showToast({ title: '头像更换成功' });
        },
        fail: (err) => {
          console.error('保存头像失败：', err);
          uni.showToast({ title: '保存失败，请重试', icon: 'none' });
        }
      });
    },

    // 加载持久化头像
    loadPersistentAvatar() {
      const fs = uni.getFileSystemManager();
      const avatarPath = `${uni.env.USER_DATA_PATH}/user_avatar.jpg`;

      try {
        fs.accessSync(avatarPath);
        this.avatarUrl = avatarPath;
      } catch (e) {
        // 无本地头像，使用默认图
      }
    }
  }
}
</script>

<style scoped>
.touxiang-page {
  padding: 20rpx;
  background-color: #fafafa;
  min-height: 100vh;
}

.back-btn {
  display: flex;
  align-items: center;
  margin-bottom: 60rpx;
}

.back-text {
  font-size: 32rpx;
  color: #333;
  margin-left: 10rpx;
}

.avatar-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 80rpx;
}

.avatar-title {
  font-size: 36rpx;
  color: #333;
  font-weight: bold;
  margin-bottom: 40rpx;
}

.avatar-wrapper {
  width: 240rpx;
  height: 240rpx;
  border-radius: 50%;
  overflow: hidden;
  border: 6rpx solid #fff;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
  position: relative;
  background-color: #f0f0f0;
}

.avatar-img {
  width: 100%;
  height: 100%;
}

.avatar-edit-icon {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 64rpx;
  height: 64rpx;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tips {
  margin-top: 20rpx;
  font-size: 26rpx;
  color: #999;
}
</style>